<template>
  <div>
    <!--    搜索表单-->
    <div>
      <el-input style="width: 240px" placeholder="请输入类名" v-model="params.name"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入备注" v-model="params.remark"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load()">
        <i class="el-icon-search"></i> 搜索
      </el-button>
      <el-button style="margin-left: 5px" type="danger" @click="reset">
        <i class="el-icon-refresh"></i>重置
      </el-button>
    </div>
    <!--增删改查-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd">新增 <i class="el-icon-edit-outline"></i></el-button>
      <el-button type="danger" @click="delBatch" class="mr-5">批量删除 <i class="el-icon-remove-outline"></i></el-button>
    </div>
    <!--表单内容-->
    <el-table :data="tableData" stripe row-key="id" default-expand-all @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column fixed
                       prop="id"
                       label="编号"
                       width="180">
      </el-table-column>
      <el-table-column prop="name" label="类名"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="createTime" label="创建时间"></el-table-column>
      <el-table-column prop="updateTime" label="更新时间"></el-table-column>

      <!--操作-->
      <el-table-column
          fixed="right"
          label="操作"
          width="300">
        <template v-slot="scope">

          <el-button @click="handleEdit(scope.row)" type="success" size="small">编辑<i class="el-icon-edit"></i>
          </el-button>


          <el-popconfirm
              class="ml-5"
              title="您确定删除这行信息吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" type="danger" size="small">删除 <i class="el-icon-remove-outline"></i>
            </el-button>
          </el-popconfirm>


          <el-button v-if="!scope.row.pid" @click="secondTitle(scope.row)" type="primary" size="small" style="margin-left: 5px">添加二级分类<i class="el-icon-edit"></i>
          </el-button>
        </template>
      </el-table-column>


    </el-table>

    <!--    分页-->
    <div style="text-align: center; ">
      <el-pagination
          background
          :current-page="params.pageNum"
          :page-size="params.pageSize"
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :total="total">
      </el-pagination>
    </div>

    <!--    新增表单-->
    <el-dialog title="用户信息" :visible.sync="dialogFormVisible" width="50%">
      <el-form label-width="80px" style="" :model="form" :rules="rules" ref="rulerForm">

        <el-form-item label="类名" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" auto-complete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>


<!--    添加二级分类-->

    <el-dialog title="用户信息" :visible.sync="secondTitleTab" width="600px">
      <el-form label-width="80px" style="" :model="secondTitleForm" :rules="rules" ref="rulerForm">

        <el-form-item label="类名" prop="name">
          <el-input v-model="secondTitleForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="secondTitleForm.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelSecond">取 消</el-button>
        <el-button type="primary" @click="secondTitleSave">确 定</el-button>
      </div>
    </el-dialog>


    <!--    //修改-->
    <el-dialog title="用户信息" :visible.sync="updateTab" width="50%">
      <el-form label-width="80px" style="" :model="updateForm" :rules="rules" ref="rulerForm">

        <el-form-item label="类名" prop="name">
          <el-input v-model="updateForm.name" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="updateForm.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelUpdate">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: "Category.vue",

  data() {

    return {
      tableData: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10,
      },
      pid: null,

      dialogFormVisible: false,
      secondTitleTab: false,
      updateTab: false,
      form: {},
      secondTitleForm: {},
      updateForm: {},

      rules: {
        name: [
          {required: true, message: '请输入类名', trigger: 'blur'},
        ],
      },

    }
  },

  created() {
    this.load();
  },


  methods: {

    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    delBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      request.post("/category/del/batch", ids).then(res => {
        if (res.code === "200") {
          this.$message.success("删除成功");
          this.load();
        } else {
          this.$message.error("批量删除失败");
        }
      })
    },

    load() {
      request.get('/category/page', {
        params: this.params
      }).then(res => {
        if (res.code === '200') {
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },

    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 10,
      }
      this.load()
    },

    handleCurrentChange(pageNum) {
      this.params.pageNum = pageNum
      this.load()
    },

    handleAdd() {
      this.dialogFormVisible = true;
      this.form = {};
    },
    secondTitle(row) {
      this.pid = row.id;
      this.secondTitleTab = true;
      this.secondTitleForm = {};
    },

    handleCancel() {
      this.dialogFormVisible = false;
      this.load()
    },
    handleCancelSecond(){
      this.secondTitleTab = false;
      this.load()
    },

    del(id) {
      request.delete('/category/delete/' + id).then(res => {
        if (res.code === '200') {
          this.$message.success('删除成功')
          this.load()
        } else {
          this.$message.error(res.msg)
        }
      })
    },

    handleEdit(row) {
      this.updateForm = row;
      this.updateTab = true;
    },


    save() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.post('/category/save', this.form).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功')
              this.dialogFormVisible = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },

    secondTitleSave() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          this.secondTitleForm.pid = this.pid
          request.post('/category/save', this.secondTitleForm).then(res => {
            if (res.code === '200') {
              this.$message.success('新增成功')
              this.secondTitleTab = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },




    update() {
      this.$refs['rulerForm'].validate((valid) => {
        if (valid) {
          request.put('/category/update', this.updateForm).then(res => {
            if (res.code === '200') {
              this.$message.success('保存成功')
              this.updateTab = false
              this.load()
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })

    },
    handleCancelUpdate() {
      this.updateTab = false
      this.load()
    },


  }
}
</script>

<style scoped>

</style>